﻿#ifndef LOGBOX_H
#define LOGBOX_H

#include <QTextEdit>
#include <QEvent>
#include <QKeyEvent>
#include <QWinEventNotifier>
#include <QAbstractNativeEventFilter>
#include <Windows.h>


class TaskEventFilter : public QAbstractNativeEventFilter
{
public:
    bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result) override
    {
        if (eventType == "windows_generic_MSG") {
            MSG *msg = static_cast<MSG*>(message);
            if (msg->message == WM_ACTIVATEAPP) {
                if (msg->wParam == TRUE) {
                    qDebug() << "App is Active";
                } else {
                    qDebug() << "App is Inactive";
                }
            }
        }
        return false;
    }
};

class GlobalEventFilter : public QObject
{
public:
    bool eventFilter(QObject *watched, QEvent *event) override
    {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *ke = static_cast<QKeyEvent*>(event);
            qDebug() << ke->text();
            return false;
        }
        return false;
    }
};


class LogEvent : public QEvent
{
public:
    explicit LogEvent(QString msg)
        : QEvent(LogType)
        , eventMsg(msg)
    {}

    static const QEvent::Type LogType;

    QString getMessage() const { return eventMsg; }

private:
    QString eventMsg;
};


class LogBox : public QTextEdit
{
    Q_OBJECT
public:
    explicit LogBox(QWidget *widget = nullptr);

public:
    bool event(QEvent *event) override;


protected:
    void paintEvent(QPaintEvent *event) override;
    bool eventFilter(QObject *watched, QEvent *event) override;
    void customEvent(QEvent *event) override;

private:
    int curId;
    static int ID;
    QWinEventNotifier *dirNotifier;
};

#endif // LOGBOX_H
